﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace Project.Engine.Utilities
{
    public class SecurityUtil
    {
        public static string MD5Encrypt(string plainText)
        {
            byte[] data, output;
            UTF8Encoding encoder = new UTF8Encoding();
            MD5CryptoServiceProvider hasher = new MD5CryptoServiceProvider();

            data = encoder.GetBytes(plainText + "project");
            output = hasher.ComputeHash(data);

            return BitConverter.ToString(output).Replace("-", "").ToLower();
        }

        public static string ResetPass(string mail) {
            string [] s = mail.Split(new char[] { '@' }, StringSplitOptions.RemoveEmptyEntries);
            if (s.Length >= 1) {
                return s[0].ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString();
            }

            return String.Empty;
        }

        public static string RemoveSqlInjection(string input)
        {
            if (!String.IsNullOrEmpty(input))
            {
                input = input.ToLower().Replace("'", "")
                         .Replace(";", "")
                         .Replace("--", "")
                         .Replace("/*", "")
                         .Replace("*/", "")
                         .Replace("xp_", "")
                         .Replace("[", "")
                         .Replace("]", "")
                         .Replace("%", "")
                         .Replace("*", "")
                         .Replace("admin", "")
                         .Replace("delete", "")
                         .Replace("drop", "")
                         .Replace("where", "")
                         .Replace("insert", "")
                         .Replace("select", "");
            }

            return input;
        }
    }
}

